package com.dimples.controller.system;

import com.dimples.common.annotation.OpsLog;
import com.dimples.common.enums.OpsLogTypeEnum;
import com.dimples.common.page.metadata.Page;
import com.dimples.common.transport.QueryRequest;
import com.dimples.common.transport.Result;
import com.dimples.controller.view.BaseWebController;
import com.dimples.system.entity.SysRole;
import com.dimples.system.entity.SysRoleMenu;
import com.dimples.system.service.SysRoleService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2021/6/26
 */
@Slf4j
@Api(tags = "角色管理")
@Validated
@RestController
@RequestMapping(value = BaseWebController.API_SYSTEM_PREFIX)
@RequiredArgsConstructor
public class SysRoleController {

    private final SysRoleService sysRoleService;

    @ApiOperation(value = "获取角色列表")
    @ApiOperationSupport(order = 1)
    @OpsLog(value = "获取角色列表", type = OpsLogTypeEnum.SELECT)
    @GetMapping("role")
    @PreAuthorize("@auth.hasPerm('role:view')")
    public Result<SysRole> findRole(SysRole sysRole, QueryRequest queryRequest) {
        Page<SysRole> result = this.sysRoleService.findRole(sysRole, queryRequest);
        return Result.success(result);
    }

    @ApiOperation(value = "查询角色")
    @ApiImplicitParam(name = "roleId", value = "角色id", paramType = "query")
    @ApiOperationSupport(order = 2)
    @OpsLog(value = "查询角色", type = OpsLogTypeEnum.SELECT)
    @GetMapping("role/{roleId}")
    @PreAuthorize("@auth.hasPerm('role:view')")
    public Result<SysRole> findRoleById(@PathVariable @NotNull(message = "不能为空") Long roleId) {
        SysRole result = this.sysRoleService.findRoleById(roleId);
        return Result.success(result);
    }

    @ApiOperation(value = "新增角色")
    @ApiOperationSupport(order = 3)
    @OpsLog(value = "新增角色", type = OpsLogTypeEnum.ADD)
    @PostMapping("role")
    @PreAuthorize("@auth.hasPerm('role:add')")
    public Result<SysRole> saveRole(@Valid @RequestBody SysRole sysRole) {
        this.sysRoleService.saveRole(sysRole);
        return Result.success();
    }

    @ApiOperation(value = "编辑角色")
    @ApiOperationSupport(order = 5)
    @OpsLog(value = "编辑角色", type = OpsLogTypeEnum.UPDATE)
    @PutMapping("role")
    @PreAuthorize("@auth.hasPerm('role:edit')")
    public Result<SysRole> updateRole(@Valid @RequestBody SysRole sysRole) {
        this.sysRoleService.updateRole(sysRole);
        return Result.success();
    }

    @ApiOperation(value = "删除角色")
    @ApiOperationSupport(order = 7)
    @OpsLog(value = "删除角色", type = OpsLogTypeEnum.DELETE)
    @DeleteMapping("role/{roleId}")
    @PreAuthorize("@auth.hasPerm('role:delete')")
    public Result<SysRole> deleteRole(@PathVariable @NotNull(message = "不能为空") Long roleId) {
        this.sysRoleService.deleteRole(roleId);
        return Result.success();
    }

    @ApiOperation(value = "批量删除角色")
    @ApiOperationSupport(order = 8)
    @OpsLog(value = "批量删除角色", type = OpsLogTypeEnum.DELETE)
    @DeleteMapping("role/batch/{roleIds}")
    @PreAuthorize("@auth.hasPerm('role:delete')")
    public Result<SysRole> deleteRoles(@PathVariable @NotBlank(message = "不能为空") String roleIds) {
        this.sysRoleService.deleteRoles(roleIds);
        return Result.success();
    }

    @ApiOperation(value = "绑定角色菜单")
    @ApiOperationSupport(order = 10)
    @OpsLog(value = "绑定角色菜单", type = OpsLogTypeEnum.ADD)
    @PostMapping("role/menu")
    @PreAuthorize("@auth.hasPerm('role:auth')")
    public Result<SysRole> bindRoleMenu(@RequestBody SysRoleMenu sysRoleMenu) {
        this.sysRoleService.bindRoleMenu(sysRoleMenu);
        return Result.success();
    }

}
